package com.youan.serve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youan.pangu.base.exception.BusinessException;
import com.youan.serve.dao.domain.Role;
import com.youan.serve.dao.domain.User;
import com.youan.serve.dao.domain.UserRole;
import com.youan.serve.dao.idao.UserMapper;
import com.youan.serve.service.IRoleService;
import com.youan.serve.service.IUserRoleService;
import com.youan.serve.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author auto_code
 * @since 2020-03-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IRoleService iRoleService;
    @Autowired
    private IUserRoleService iUserRoleService;



    @Override
    public List<User> getUsersByConditions(String orgId, String userName, String realName, String roleId) throws Exception {
        List<User> userList2 = new ArrayList<User>();
        List<User> userList = new ArrayList<User>();
        if((userName==null || userName.equals("")) && (realName!=null && !realName.equals("")) ){
            userList = this.list(new LambdaQueryWrapper<User>().eq(User::getRealName, realName).eq(User::getOrgId, orgId));
        }
        else if((userName!=null && !userName.equals("")) && (realName==null || realName.equals(""))){
            userList = this.list(new LambdaQueryWrapper<User>().eq(User::getUserName, userName).eq(User::getOrgId, orgId));
        }
        else if((userName!=null && !userName.equals("")) && (realName!=null && !realName.equals(""))){
            userList = this.list(new LambdaQueryWrapper<User>().eq(User::getUserName, userName).eq(User::getRealName, realName).eq(User::getOrgId, orgId));
        }
        else if((userName==null || userName.equals("")) && (realName==null || realName.equals(""))){
            userList = this.list(new LambdaQueryWrapper<User>().eq(User::getOrgId, orgId));
        }
        this.setUserRoleIdandRoleName(userList);
        if(roleId==null || roleId.equals("")){
            return userList;
        }
        for(User user : userList){
            if(user.getRoleId().equals(roleId)){
                userList2.add(user);
            }
        }
        return userList2;
    }


    public List<User> setUserRoleIdandRoleName(List<User> userList) throws Exception{
        List<Role> roleList = iRoleService.list();
        List<UserRole> userRoleList = iUserRoleService.list();

        for(User user : userList){
            for(UserRole userRole : userRoleList){
                if(user.getUserId().equals(userRole.getUserId())){
                    for(Role role : roleList){
                        if(role.getRoleId().equals(userRole.getRoleId())){
                            user.setRoleName(role.getRoleName());
                            user.setRoleId(role.getRoleId());
                        }
                    }
                }
            }
        }
        return userList;
    }

    @Override
    public boolean judgeUser(User user) throws Exception{
        if (user == null) {
            throw new BusinessException("对象为空！");
        }
        if (user.getUserName() == null || user.getUserName().equals("")) {
            throw new BusinessException("用户名为空！");
        }
        if (user.getUserPassword() == null || user.getUserPassword().equals("")) {
            throw new BusinessException("密码为空！");
        }
        if(!user.getUserPassword().equals(user.getUserPasswordAgain())){
            throw new BusinessException("两次输入的密码不一致！");
        }
        if (user.getRealName() == null || user.getRealName().equals("")) {
            throw new BusinessException("真实姓名为空！");
        }
        if (user.getStatus() == null || user.getStatus().equals("")) {
            throw new BusinessException("状态为空！");
        }
        List<User> userList = this.list(new LambdaQueryWrapper<User>().eq(User::getUserName, user.getUserName()).ne(User::getUserId, user.getUserId()));
        if(userList!=null && userList.size()>0){
            throw new BusinessException("存在重复的用户名！");
        }
        return true;
    }
}
